﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FastReport.Data;
using Leadtools;
using Leadtools.Codecs;
using Leadtools.WinForms;
using SmallERP.COM;
using SmallERP.MODEL.FenPian;
using SmallERP.Bll.FenPian;
using SmallERP.MODEL.FoundationModel;
using Smallerp.Contracts.DataContracts;
using Smallerp.Contracts;

namespace SmallERP.FenPian.Forms
{
    public partial class FrmReturn : Form
    {
        public FrmReturn()
        {
            InitializeComponent();
           
        }
        InnerReturnBLL _bll ;
        List<InnerReturn_InfoDCT> CurrPhotoList = new List<InnerReturn_InfoDCT>();
        private void textEditPhotoId_KeyDown(object sender, KeyEventArgs e)
        {

        }
        private static FrmReturn _instance;
        public static FrmReturn GetInstance
        {
            get
            {
                if (_instance == null)
                {
                    return new FrmReturn();
                }
                return _instance;
            }
        }
        public IOperateDBFenPian OperateDb { get; set; }
        private void simpleButtonOK_Click(object sender, EventArgs e)
        {
            if (CurrPhotoList.Count() > 0)
            {
                // List<InnerReturn_Info> list = CurrPhotoList;
                Dictionary<int, int> outDict = new Dictionary<int, int>();
                foreach (InnerReturn_InfoDCT info in CurrPhotoList)
                {
                    if (string.IsNullOrEmpty(info.Remark.Trim()) || string.IsNullOrEmpty(info.CreateUserName.Trim()) || string.IsNullOrEmpty(info.ResponseUserName.Trim()))
                    {
                        MessageBox.Show(info.PhotoId + "中的返单人责任人和原因不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        return;
                    }

                }
                int intRtn = _bll.CreateInnerReturn(CurrPhotoList, out outDict);
                if (intRtn == 0)
                {


                    MessageBox.Show("全部照片返工完成！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    FrmShowPaperCode from = new FrmShowPaperCode(outDict);
                    from.ShowDialog();
                    gridControlphotos.DataSource = null;
                    gridControlphotos.RefreshDataSource();
                }
                else
                {

                    MessageBox.Show("连接失败，请检查网络连接", "提示", MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("请输入返单照片！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            CurrPhotoList = new List<InnerReturn_InfoDCT>();
        }

        private void simpleButtonchakan_Click(object sender, EventArgs e)
        {

            List<string> displayString = new List<string>();
            foreach (InnerReturn_InfoDCT c in CurrPhotoList)
            {

                displayString.Add(c.PicPath);
                displayString.Add(c.PhotoId.ToString());

            }
            if (displayString.Count == 0)
            {
                MessageBox.Show("请先填写返单照片！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            else
            {
                try
                {
                    BigPicViewer viewer = new BigPicViewer();
                    viewer.PhotoPaths = displayString;
                    viewer.CurrentIndex = 0;
                    viewer.ShowDialog();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("读取照片失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }
        }

        private void simpleButtonChangeDisplay_Click(object sender, EventArgs e)
        {
            if (splitContainerListOrImage.Panel1Collapsed == false)
            {
                splitContainerListOrImage.Panel1Collapsed = true;
                splitContainerListOrImage.Panel2Collapsed = false;
                ///显示缩略图
                rasterThumbnailBrowserPicView.Items.Clear();
                foreach (InnerReturn_InfoDCT photo in this.CurrPhotoList)
                {
                    try
                    {
                        RasterCodecs.Startup();
                        RasterCodecs codecs = new RasterCodecs();
                        CodecsThumbnailOptions options = new CodecsThumbnailOptions();
                        options.Resample = true;
                        options.LoadStamp = true;
                        options.MaintainAspectRatio = true;
                        // options.
                        RasterImage img;
                        try
                        {
                            img = codecs.ReadThumbnail(photo.PicPath, options, 1);
                        }
                        catch
                        {
                            //为图片设置Stamp
                            RasterImage imgInfo = codecs.Load(photo.PicPath);
                            Rectangle rc = RasterImage.CalculatePaintModeRectangle(imgInfo.Width, imgInfo.Height, new Rectangle(0, 0, 128, 128), RasterPaintSizeMode.FitAlways, RasterPaintAlignMode.Near, RasterPaintAlignMode.Near);
                            codecs.Options.Jpeg.Save.StampWidth = rc.Width;
                            codecs.Options.Jpeg.Save.StampHeight = rc.Height;
                            codecs.Options.Jpeg.Save.SaveWithStamp = true;
                            codecs.Options.Jpeg.Save.StampBitsPerPixel = 24;
                            codecs.Save(imgInfo, photo.PicPath, RasterImageFormat.Jpeg, 0);
                            //重新读缩略图
                            img = codecs.ReadThumbnail(photo.PhotoId.ToString(), options, 1);
                        }
                        //int index = photoPath.LastIndexOf("\\");
                        //string fileName = photoPath.Substring(index + 1);
                        RasterImageListItem item = new RasterImageListItem(img, 1, photo.PhotoId.ToString());

                        item.Tag = photo.PhotoId.ToString();
                        rasterThumbnailBrowserPicView.Items.Add(item);

                    }
                    catch { return; }
                }
            }
            else
            {
                splitContainerListOrImage.Panel1Collapsed = false;
                splitContainerListOrImage.Panel2Collapsed = true;
            }
        }

        private void FrmReturn_Load(object sender, EventArgs e)
        {
            _bll = new InnerReturnBLL(OperateDb);
            foreach (string str in _bll.GetAllName())
            {
                comboBoxZeRenRen.Items.Add(str);
                repositoryItemComboBoxZerenren.Items.Add(str);
            }
            foreach (string str in _bll.GetAllName())
            {
                comboBoxFanDanRen.Items.Add(str);
                repositoryItemComboBoxCreateMan.Items.Add(str);
            }
            foreach (string str in _bll.GetAllPaperWasteType())
            {
                comboBoxRemark.Items.Add(str);
                repositoryItemComboBoxReamark.Items.Add(str);
            }

        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(textEditPhotoId.Text.Trim()))
            {
                MessageBox.Show("请输入ID！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;

            }
            int length = textEditPhotoId.Text.Trim().Length;

            int photoid = Convert.ToInt32(textEditPhotoId.Text.Trim());
            InnerReturn_InfoDCT view = _bll.GetInnerView(photoid);
            if (view != null)
            {
                int intWaitPB = (int)Status.PhotoStatus.WaitPB;
                int intWaitProductBigPic = (int)Status.PhotoStatus.WaitProductBigPic;
                int intWaitMove = (int)Status.PhotoStatus.WaitMove;
                if (view.PhotoStatus == intWaitPB)
                {
                    if (MessageBox.Show("照片现在等待排版是否返单", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) ==
                        System.Windows.Forms.DialogResult.OK)
                    {

                    }
                    else
                    {
                        return;
                    }


                }
                else if (view.PhotoStatus == intWaitProductBigPic)
                {
                    if (MessageBox.Show("照片正在生成大图是否返单", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) ==
                       System.Windows.Forms.DialogResult.OK)
                    {

                    }
                    else
                    {
                        return;
                    }

                }
                else if (view.PhotoStatus == intWaitMove)
                {
                    if (MessageBox.Show("照片正在等待移动是否返单", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) ==
                      System.Windows.Forms.DialogResult.OK)
                    {

                    }
                    else
                    {
                        return;
                    }
                }


                InnerReturn_InfoDCT viewNow = CurrPhotoList.FirstOrDefault(p => p.PhotoId == photoid);
                if (viewNow == null)
                {
                    view.ResponseUserName = this.comboBoxZeRenRen.Text;
                    view.CreateUserName = this.comboBoxFanDanRen.Text;
                    view.Remark = this.comboBoxRemark.Text;
                    CurrPhotoList.Add(view);
                    gridControlphotos.DataSource = CurrPhotoList;
                    gridControlphotos.RefreshDataSource();
                }
                else
                {
                    MessageBox.Show("照片已经添加！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }
            else
            {
                MessageBox.Show("照片未打印完成或者该产品不在生产中！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            textEditPhotoId.Text = "";
        }

        private void simpleButtonCancel_Click(object sender, EventArgs e)
        {
            CurrPhotoList.Clear();
            gridControlphotos.DataSource = CurrPhotoList;
            gridControlphotos.RefreshDataSource();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            List<InnerReturn_InfoDCT> listInner = new List<InnerReturn_InfoDCT>();
            InnerReturn_InfoDCT inner;
            foreach (int list in gridViewPhotos.GetSelectedRows())
            {
                inner = (InnerReturn_InfoDCT)gridViewPhotos.GetRow(list);

                listInner.Add(inner);
            }
            CurrPhotoList = CurrPhotoList.Except(listInner).ToList();
            gridControlphotos.DataSource = CurrPhotoList;
            gridControlphotos.RefreshDataSource();
        }

    }
}
